package com.judian.smart.model;

import com.judian.smart.constant.ServiceType;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

// 语义结果抽象类
public class SemanticResult {
    public final static String TAG = SemanticResult.class.getSimpleName();

    public final static int MUSIC_MODE = 0;
    public final static int RADIO_MODE = 1;

    private final static String KEY_TEXT = "text";
    private final static String KEY_ANSWER = "answer";
    private final static String KEY_RESULT = "result";
    private final static String KEY_SEMANTIC = "semantic";
    private final static String KEY_SLOTS = "slots";
    private final static String KEY_OPERATION = "operation";
    private final static String KEY_SID = "sid";
    private final static String KEY_EMOTION = "emotion";
    private final static String KEY_INTENT = "intent";
    private final static String KEY_DATA = "data";
    private final static String EMOTION_DEFAULT = "default";
    private final static String EMOTION_ANGRY = "angry";
    public final static String KEY_UUID = "uuid";


    private String sid;
    private String uuid;
    private ServiceType service;
    private String operation;
    private String answerText = "";
    private String emotion = "";
    private JSONObject semantic = null;
    private JSONObject data = null;
    private JSONObject json;
    public static int CURRENT_MODE = -1; //当前播放模式

    public SemanticResult(String service, JSONObject json) {
        try {
            this.service = ServiceType.getServiceType(service);
            if (this.service == ServiceType.MUSICX) {
                CURRENT_MODE = MUSIC_MODE;
            }
            if (this.service == ServiceType.INTERNETRADIO) {
                CURRENT_MODE = RADIO_MODE;
            }
            this.json = json;
            if (!service.equals("scheduleX") && !service.equals("musicX")) {  //如果是音乐和闹钟不做json处理
                this.sid = json.getString(KEY_SID);
                this.uuid = json.getString(KEY_UUID);
                boolean isAIUI3_0 = json.has(KEY_OPERATION);
                if (isAIUI3_0) {
                    this.semantic = json.optJSONObject(KEY_SEMANTIC);
                } else {
                    JSONObject fakeSemantic = json.getJSONArray(KEY_SEMANTIC).getJSONObject(0);
                    JSONArray slots = fakeSemantic.optJSONArray(KEY_SLOTS);
                    JSONObject fakeSlots = new JSONObject();
                    for (int index = 0; index < slots.length(); index++) {
                        JSONObject item = slots.optJSONObject(index);
                        String name = item.optString("name");
                        String value = item.optString("value");
                        fakeSlots.put(name, value);
                    }
                    fakeSemantic.put(KEY_SLOTS, fakeSlots);
                    this.semantic = fakeSemantic;
                }
                this.operation = isAIUI3_0 ? json.getString(KEY_OPERATION) : this.semantic.getString(KEY_INTENT);
                this.data = json.optJSONObject(KEY_DATA);
                if (json.has(KEY_ANSWER)) {
                    JSONObject answerJson = json.getJSONObject(KEY_ANSWER);
                    this.answerText = answerJson.optString(KEY_TEXT, "");
                    this.emotion = answerJson.optString(KEY_EMOTION, "");
                    if (EMOTION_DEFAULT.equals(emotion) || EMOTION_ANGRY.equals(emotion)) {
                        this.emotion = "";
                    }
                } else {
                    this.answerText = "";
                    this.emotion = "";
                }
            }
            manualReturnService();  //判断语义是否包含相关内容做人工"service"赋值
            saveAnswer();  //修改回答内容
        } catch (JSONException e) {
            e.printStackTrace();
            if (answerText.contains("返回桌面") || answerText.contains("返回主界面")) {
                answerText = "好的主人";
            }
            if (answerText.contains("声音") || answerText.contains("音量") || answerText.contains("静音")) {
                String[] array = new String[]{"好的主人", "没问题"};
                int randomIndex = (int) (Math.random() * array.length);
                answerText = array[randomIndex];
            }
            answerText = "";
        }
    }

    private void saveAnswer() {
        String text = json.optString("text");//说话的文本
        if (text.contains("返回主界面") || text.contains("返回桌面") || text.contains("打开蓝牙")
                || text.contains("关闭蓝牙") || text.contains("切换本地")) {
            answerText = "好的主人";
        }
        if (text.contains("切换蓝牙")) {
            answerText = "正在重新连接蓝牙";
        }
        if (text.contains("随机")) {
            answerText = "已为您将播放模式设置为\"随机播放\"模式";
        }
        if (text.contains("单曲循环")) {
            answerText = "已为您将播放模式设置为\"单曲循环\"模式";
        }
        if (text.contains("继续")) {
            answerText = "已为您继续播放器";
        }
        if (text.contains("暂停")) {
            answerText = "已为您暂停播放器";
        }
        if (text.contains("停止")) {
            answerText = "已为您停止播放器";
        }
        if (text.contains("上一曲") || text.contains("上一首")) {
            answerText = "将为您播放上一首歌曲";
        }
        if (text.contains("下一曲") || text.contains("下一首") || text.contains("换一首") || text.contains("放一首")) {
            answerText = "将为您播放下一首歌曲";
        }
        if (text.contains("播放音乐") || text.contains("推荐音乐") || text.contains("推荐歌曲") ||
                text.contains("播放歌曲") || text.contains("来点音乐")) {
            answerText = "好的";
        }
        if (text.matches("打开.+灯光") || text.matches("开启.+灯光")) {
            answerText = "已为您打开灯";
        }
        if (text.matches("关闭.+灯光") || text.matches("关掉.+灯光")) {
            answerText = "已为您关闭灯";
        }
        if ((text.matches("播放.+") || text.matches("我想听.+"))
                && !text.equals("播放音乐")) {
            String[] array = new String[]{"好的", "没问题"};
            int randomIndex = (int) (Math.random() * array.length);
            answerText = array[randomIndex];
        }
    }

    //人工设置服务类型
    private void manualReturnService() {
        String answer = json.optString("text");
        String[] semantics = {"随机", "我想听", "播放", "推荐音乐", "推荐歌曲", "换一首", "放一首",
                "下一首", "下一曲", "上一首", "上一曲","单曲循环","列表循环"};
        for (String s : semantics) {
            if (answer.contains(s)) {
                CURRENT_MODE = MUSIC_MODE;
                this.service = ServiceType.MUSICX;
            }
        }
        if (answer.equals("我想听电台") || answer.matches("我想听.+电台")) {
            CURRENT_MODE = RADIO_MODE;
            this.service = ServiceType.INTERNETRADIO;
        }
    }


    public ServiceType getServiceType() {
        return service;
    }

    public String getAnswerText() {
        if (answerText == null || answerText.equals("null")) {
            answerText = "";
        }
        return answerText;
    }

    public String getEmotion() {
        return emotion;
    }

    public JSONObject getSemantic() {
        return semantic;
    }


    public JSONArray getResult() {
        if (data == null) {
            return null;
        } else {
            return data.optJSONArray(KEY_RESULT);
        }
    }

    public String getSid() {
        return sid;
    }

    public String getUUID() {
        return uuid;
    }

    public String getOperation() {
        return operation;
    }


    public JSONObject getJson() {
        return json;
    }
}
